{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "93103597-996b-46ec-9804-63164b775896",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from pprint import pprint\n",
    "from pathlib import Path\n",
    "\n",
    "import duckdb\n",
    "import ipywidgets as widgets\n",
    "import json\n",
    "\n",
    "from mosaic_widget import MosaicWidget"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ed9edb9e-4ed2-4e5e-b44c-81a2c7a73af4",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# Optional: enable logging in Mosaic widget\n",
    "import logging\n",
    "\n",
    "logging.basicConfig(level=logging.INFO)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "71be7064-deb8-4b5e-b86c-7c73c86220cf",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Change working directory to mosaic root\n",
    "%cd ../.."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5f46684a-5a00-4db2-9a1e-7b394a84fc21",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "con = duckdb.connect()\n",
    "con.execute(\"LOAD httpfs\");"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4da7f0fd-ec48-4ca9-b927-cbf9b0a5d8b8",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "mosaic = MosaicWidget(con=con)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "72656409-7087-4394-bf73-24fe5353cea3",
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_specs(folder_path):\n",
    "    folder = Path(folder_path)\n",
    "    for file_path in sorted(folder.glob(\"*.json\")):\n",
    "        data = json.loads(file_path.read_text())\n",
    "        meta = data.get(\"meta\")\n",
    "        if meta:\n",
    "            yield (meta.get(\"title\"), file_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "211e4826-c2c6-4af2-a46f-1786d4600c73",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "weather = Path(\"specs/json/weather.json\")\n",
    "\n",
    "dropdown = widgets.Dropdown(\n",
    "    options=get_specs(\"specs/json\"),\n",
    "    value=weather,\n",
    "    description=\"Example:\",\n",
    ")\n",
    "\n",
    "\n",
    "def on_change(change):\n",
    "    open_spec(change[\"new\"])\n",
    "\n",
    "\n",
    "def open_spec(spec):\n",
    "    mosaic.spec = json.loads(spec.read_text())\n",
    "\n",
    "\n",
    "dropdown.observe(on_change, \"value\")\n",
    "\n",
    "open_spec(weather)\n",
    "\n",
    "output = widgets.Output()\n",
    "\n",
    "\n",
    "@output.capture(clear_output=True)\n",
    "def handle_change(change):\n",
    "    pprint(change.new)\n",
    "\n",
    "\n",
    "mosaic.observe(handle_change, names=[\"params\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d192b9ff-2592-4683-b22d-b87994196379",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "widgets.VBox([dropdown, mosaic, output])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "29babf14-24dc-4fc9-befb-ccda6eaa4781",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
